package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import model.Process;
import model.ProductType;
import service.Service;

/**
 * 
 * @author Ceslovas Lopan
 * 
 */
public class ManageData extends JFrame {
	private final Controller controller = new Controller();
	private JButton btnAddType, btnEditType, btnRemove, btnAddProcess,
			btnEditProcess, btnRemoveProcess;
	private JList lstTypes, lstProcesses;

	public ManageData() {
		setSize(567, 461);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(100, 100);
		setResizable(false);
		setTitle("Manage data");
		getContentPane().setLayout(new BorderLayout(5, 5));

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(10, 1, 0, 0));

		btnAddType = new JButton("Add Type");
		panel.add(btnAddType);

		btnEditType = new JButton("Edit Type");
		panel.add(btnEditType);

		btnRemove = new JButton("Remove Type");
		panel.add(btnRemove);

		JPanel panel1 = new JPanel();
		getContentPane().add(panel1, BorderLayout.EAST);
		panel1.setLayout(new GridLayout(10, 1, 0, 0));

		btnAddProcess = new JButton("Add Process");
		panel1.add(btnAddProcess);

		btnEditProcess = new JButton("Edit Process");
		panel1.add(btnEditProcess);

		btnRemoveProcess = new JButton("Remove Process");
		panel1.add(btnRemoveProcess);

		JPanel panel2 = new JPanel();
		getContentPane().add(panel2, BorderLayout.CENTER);
		panel2.setLayout(new GridLayout(0, 2, 5, 0));

		JPanel panel3 = new JPanel();
		panel2.add(panel3);
		panel3.setLayout(new BorderLayout(0, 0));

		JPanel panel5 = new JPanel();
		panel3.add(panel5, BorderLayout.NORTH);

		JLabel lblProductTypes_1 = new JLabel("Product Types");
		panel5.add(lblProductTypes_1);

		JScrollPane scrollPane = new JScrollPane();
		panel3.add(scrollPane, BorderLayout.CENTER);

		lstTypes = new JList(Service.getInstance().getProductTypes().toArray());

		scrollPane.setViewportView(lstTypes);

		JPanel panel4 = new JPanel();
		panel2.add(panel4);
		panel4.setLayout(new BorderLayout(0, 0));

		JPanel panel6 = new JPanel();
		panel4.add(panel6, BorderLayout.NORTH);

		JLabel lblProcesses = new JLabel("Processes");
		panel6.add(lblProcesses);

		JScrollPane scrollPane1 = new JScrollPane();
		panel4.add(scrollPane1, BorderLayout.CENTER);

		lstProcesses = new JList(Service.getInstance().getProcesses().toArray());

		scrollPane1.setViewportView(lstProcesses);

		btnAddType.addActionListener(controller);
		btnEditType.addActionListener(controller);
		btnRemove.addActionListener(controller);

		btnAddProcess.addActionListener(controller);
		btnEditProcess.addActionListener(controller);
		btnRemoveProcess.addActionListener(controller);
	}

	public void updateLists() {
		lstTypes.setListData(Service.getInstance().getProductTypes().toArray());
		lstProcesses
				.setListData(Service.getInstance().getProcesses().toArray());
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnAddType) {
				AddEditType aet = new AddEditType(ManageData.this);
				aet.setVisible(true);
			} else if (e.getSource() == btnEditType) {
				Object selected = lstTypes.getSelectedValue();
				AddEditType aet;

				if (selected != null) {
					aet = new AddEditType(ManageData.this,
							(ProductType) selected);
					aet.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}

			} else if (e.getSource() == btnRemove) {
				Object selected = lstTypes.getSelectedValue();

				if (selected != null) {
					if (((ProductType) selected).getStacks().size() == 0) {
						Service.getInstance().removeProductType(
								(ProductType) selected);

						updateLists();
					} else {
						JOptionPane
								.showMessageDialog(
										ManageData.this,
										"This product type is used by some stacks, this product type cannot be removed",
										"Error", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			} else if (e.getSource() == btnAddProcess) {
				AddEditProcess aep = new AddEditProcess(ManageData.this);
				aep.setVisible(true);
			} else if (e.getSource() == btnEditProcess) {
				Object selected = lstProcesses.getSelectedValue();
				AddEditProcess aep;

				if (selected != null) {
					aep = new AddEditProcess(ManageData.this,
							(Process) selected);
					aep.setVisible(true);
				} else {
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			} else if (e.getSource() == btnRemoveProcess) {
				Object selected = lstProcesses.getSelectedValue();

				if (selected != null) {
					if (!Service.getInstance()
							.processIsUsed((Process) selected)) {
						Service.getInstance().removeProcess((Process) selected);
						updateLists();
					} else {
						JOptionPane
								.showMessageDialog(
										ManageData.this,
										"This product type is in some recipes, please remove product types first",
										"Error", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(ManageData.this,
							"Please select type", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
}
